import { INestApplication, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { MyResponse, MyWsErrorResponse, PaginationData } from "./common/model";

export function setupSwagger(app: INestApplication, configService: ConfigService): void {
    if (!configService.get<boolean>("ENABLE_SWAGGER")) return;

    const documentBuilder = new DocumentBuilder()
        .setTitle("Deice Manage")
        .setDescription("Deice Manage API document")
        .setVersion("1.0");

    // auth security
    // documentBuilder.addSecurity("auth", {
    //     description: "输入令牌（Enter the token）",
    //     type: "http",
    //     scheme: "bearer",
    //     bearerFormat: "JWT",
    // });

    const document = SwaggerModule.createDocument(app, documentBuilder.build(), {
        ignoreGlobalPrefix: false,
        extraModels: [MyResponse, MyWsErrorResponse, PaginationData],
    });

    SwaggerModule.setup("api_docs", app, document, {
        swaggerOptions: {
            persistAuthorization: true, // 保持登录
        },
    });

    // started log
    const logger = new Logger("SwaggerModule");
    logger.log(`Document running on http://127.0.0.1:${configService.get("HTTP_PROT")}/api_docs`);
}
